import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:secend_flutter/bean/travel_model_bean.dart';
import 'package:secend_flutter/bean/travel_tab_model_bean.dart';
import 'package:secend_flutter/bottom_bar/travel_item_tab_page.dart';
import 'package:secend_flutter/dao/tao_ean.dart';
import 'package:secend_flutter/dao/travel_tab_dao.dart';
import 'package:secend_flutter/pic/instagram_filter_selection.dart';
import 'package:secend_flutter/video_clip/clipper_view.dart';

class CameraPage extends StatefulWidget {
  const CameraPage({Key? key}) : super(key: key);

  @override
  _CameraPageState createState() => _CameraPageState();
}

class _CameraPageState extends State<CameraPage>
    with SingleTickerProviderStateMixin {
  TabController? _controller;
  List<TravelTab> travelTabs = [];
  TravelTabModel? _tabModel;
  late File _image;
  final picker = ImagePicker();
  @override
  void initState() {
    TravelTabDao.fetch().then((TravelTabModel value) {
      setState(() {
        travelTabs = value.tabs!;
        _tabModel = value;
        _controller = TabController(length: travelTabs.length, vsync: this);
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    _controller!.dispose();
    super.dispose();
  }
  Future getVideo() async {
    final pickedFile = await picker.getVideo(source: ImageSource.camera);

    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);
        print(_image.absolute);
      } else {
        print('No video selected.');
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: [
        if (_controller == null)
          const Text('正在加载')
        else
          TabBar(
            labelColor: Colors.black,
            isScrollable: true,
            labelPadding: const EdgeInsets.fromLTRB(15, 0, 15, 5),
            padding: const EdgeInsets.only(top: 25),
            indicator: const UnderlineTabIndicator(
                insets: EdgeInsets.only(bottom: 6),
                borderSide: BorderSide(width: 2, color: Colors.blueAccent)),
            controller: _controller,
            tabs: travelTabs
                .map<Tab>((TravelTab e) => Tab(
                      text: e.labelName,
                    ))
                .toList(),
          ),
        //旅拍模块
        Flexible(
            flex: 1,
            child: _controller == null
                ? const Text('网络缓慢...')
                : TabBarView(
                    controller: _controller,
                    children: travelTabs.map((TravelTab item) {
                      return TravelListPage(travelUrl:_tabModel!.url,groupChannelCode:item.groupChannelCode , params: _tabModel!.params!);
                    }).toList()))
      ],
    ),
      //视频裁剪
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          showModalBottomSheet(
              context: context,
              shape: const RoundedRectangleBorder(
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(20.0),
                    topRight: Radius.circular(20.0)),
              ),
              builder: (BuildContext context) {
                return Container(
                  height: 300,
                  child: Column(
                    children: [
                      Container(
                        decoration: const BoxDecoration(
                          border: Border(
                              bottom: BorderSide(
                                  width: 1.0, color: Colors.black12)),
                        ),
                        height: 80,
                        width: 750,
                        child: InkWell(
                          onTap: () {
                            Navigator.pop(context);
                            Tiaboon.push(
                                context, ExampleInstagramFilterSelection());
                          },
                          child: const Center(
                            child: Text(
                              '为照片添加滤镜',
                              style: TextStyle(
                                  fontSize: 20, fontWeight: FontWeight.bold),
                            ),
                          ),
                        ),
                      ),
                      Container(
                        decoration: const BoxDecoration(
                          border: Border(
                              bottom: BorderSide(
                                  width: 1.0, color: Colors.black12)),
                        ),
                        height: 80,
                        width: 750,
                        child: InkWell(
                          onTap: () {
                            getVideo();
                          },
                          child: const Center(
                            child: Text(
                              '拍摄视频',
                              style: TextStyle(
                                  fontSize: 20, fontWeight: FontWeight.bold),
                            ),
                          ),
                        ),
                      ),
                      Container(
                        height: 80,
                        width: 750,
                        child: InkWell(
                          onTap: () async {
                            // Navigator.pop(context);
                            FilePickerResult? result =
                            await FilePicker.platform.pickFiles(
                              type: FileType.video,
                              allowCompression: false,
                            );
                            if (result != null) {
                              File file = File(result.files.single.path!);
                              Navigator.of(context).push(
                                MaterialPageRoute(builder: (context) {
                                  return ClipperView(file);
                                }),
                              );
                            }
                          },
                          child: const Center(
                            child: Text(
                              '剪辑视频',
                              style: TextStyle(
                                  fontSize: 20, fontWeight: FontWeight.bold),
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                );
              });
        },
      ),
    );
  }
}
